#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   MapLogic
@Time    :   2023/03/13 18:22:25
@author  :   Eval
@Version :   1.0
@Describe:   游戏地图逻辑(运行时)
'''
from map.mapManage.mapMgr import *
from lib.scripts.GC import *

class MapLogic:
    def __init__(self,parent) -> None:
        self.parent = parent
        # 当前楼层名称
        self.floorName = GC.GC_GlobalScripts.get("MainFloor")
        # 存储所有去过的楼层数据 : 楼层名: 楼层数据
        self.GameLoadMap = {}

    def getFloor(self,fName:str=None):
        """根据地图名称从缓存里面拿地图,如果为空,则默认当前楼层"""
        if fName is None:
            fName = self.floorName
        if self.GameLoadMap.get(fName) is None:
            map2d = MapMgr.getByMapName(fName).data
            self.GameLoadMap[fName] = map2d
        
        return self.GameLoadMap[fName]["map"]
    
    def getFloorEvents(self):
        """返回当前楼层的事件"""
        return self.GameLoadMap[self.floorName]["events"]
    
    def updataFloor(self,fName:str):
        """更新指定楼层的地图缓存"""
        if fName is None:
            return
        self.parent.GInit.clearCache()

    def updataFPoint(self,x:int,y:int,newPoint:int=0,fName:str=None):
        """更新指定楼层的某个坐标点"""
        if fName is None:
            fName = self.floorName
        # 修改的是控制渲染的缓存
        self.parent.GInit.modifyCachePos(y,x,newPoint)
        # 修改实际的底层缓存,
        self.GameLoadMap[fName]["map"][x][y] = int(newPoint)

    def changeFloor(self,newFName:str,loc:list)->bool:
        """切换地图"""
        if MapMgr.getByMapName(newFName).type == DiaLogResult.false:
            return False
        # 将当前楼层的缓存清除
        self.updataFloor(self.floorName)
        # 清空当前楼层的怪物信息
        GC.GC_FloorMonsterData.clear()
        self.floorName = newFName
        self.parent.hero.setHeroPos(loc=loc)
        self.parent.hero.stopMove()
        return True